%%%-------------------------------------------------------------------
%%% @doc  
%%% @end
%%%-------------------------------------------------------------------
-module(dpc_sup).

-behaviour(supervisor).
%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% External exports
%%--------------------------------------------------------------------
-export([
	 start_link/1
        ]).

%%--------------------------------------------------------------------
%% Internal exports
%%--------------------------------------------------------------------
-export([
	 init/1
        ]).

%%--------------------------------------------------------------------
%% Macros
%%--------------------------------------------------------------------
-define(SERVER, ?MODULE).

%%--------------------------------------------------------------------
%% Records
%%--------------------------------------------------------------------

%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% @doc Starts the supervisor.
%% @spec start_link(StartArgs) -> {ok, pid()} | Error
%% @end
%%--------------------------------------------------------------------
start_link(StartArgs) ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%====================================================================
%% Server functions
%%====================================================================
%%--------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok,  {SupFlags,  [ChildSpec]}} |
%%          ignore                          |
%%          {error, Reason}   
%%--------------------------------------------------------------------
init([]) ->
  RestartStrategy    = one_for_one,
  MaxRestarts        = 1000,
  MaxTimeBetRestarts = 3600,
  
  SupFlags = {RestartStrategy, MaxRestarts, MaxTimeBetRestarts},
  
  Resources = case catch dict:fetch(client_resources, utils:config(config_server:config_file())) of
		{'EXIT', _} ->
			case lists:keysearch(client_resources, 1, config_server:defaults()) of
    		{value, {client_resources, Value}} ->
    			Value;
    		false ->
    			1
    	end;
		R ->
			R
	end,
  
	ResourceSpecs = resource_specs(Resources, []),
	
  ChildSpecs =
  	[
  	 {config_server,
  	  {config_server, start_link, []},
  	  permanent,
  	  1000,
  	  worker,
  	  [config_server]},
  	 {dpc_server,
  	  {dpc_server, start_link, []},
  	  permanent,
  	  1000,
  	  worker,
  	  [dpc_server]},
  	 {resource_server,
  	  {resource_server, start_link, []},
  	  permanent,
  	  1000,
  	  worker,
  	  [resource_server]},
  	 {process_server,
  	  {process_server, start_link, []},
  	  permanent,
  	  1000,
  	  worker,
  	  [process_server]}
  	 | ResourceSpecs],
	
  {ok, {SupFlags, ChildSpecs}}.

%%====================================================================
%% Internal functions
%%====================================================================

resource_specs(0, Specs) ->
	Specs;
resource_specs(N, Specs) ->
	resource_specs(N - 1, [{resource:local_name(N),
		{resource, start_link, [N]},
		permanent,
		1000,
		worker,
		[resource]} | Specs]).